import Mock from 'mockjs';

Mock.setup({
    timeout: '500-800',
});

// 角色列表数据
const roles = [
    {
        key: '0',
        id: '1',
        name: '商品管理员',
        description: '只能查看及操作商品',
        userNum: '0',
        createDate: '2024-2-21 20:12:00',
        ifused: true,
    },
    {
        key: '1',
        id: '2',
        name: '订单管理员',
        description: '只能查看及操作订单',
        userNum: '0',
        createDate: '2024-2-22 18:34:00',
        ifused: true,
    },
    {
        key: '2',
        id: '3',
        name: '超级管理员',
        description: '拥有所有查看和操作功能',
        userNum: '0',
        createDate: '2024-2-24 23:52:00',
        ifused: true,
    }
];

Mock.mock(/\/role\/list/, 'get', () => {
    console.log('/role/list接口拦截');
    return { code: 1, msg: '获取成功', result: roles };
  });
  
  Mock.mock(/\/role\/add/, 'post', (options) => {
    console.log('options', options);
    const { body } = options;
    roles.push(JSON.parse(body));
  
    return { code: 1, msg: '添加成功', result: roles };
  });
  
  Mock.mock(/\/role\/edit/, 'post', (options) => {
    console.log('/role/edit接口拦截');
    const { body } = options;
    const index = roles.findIndex((ele) => ele.id === body.id)+1;
    roles.splice(index, 1, JSON.parse(body));
  
    return { code: 1, msg: '修改成功', result: roles };
  });
  
  Mock.mock(RegExp(`/role/get.*`), 'get', (options) => {
    console.log('/role/get/:id接口拦截');
    const arr = options.url.split('/');
    const id = arr[arr.length - 1];
    const data = roles.find((ele) => ele.id === id);
    return { code: 1, msg: '获取成功', result: data };
  });
  
  Mock.mock(RegExp(`/role/delete.*`), 'delete', (options) => {
    const arr = options.url.split('/');
    const id = arr[arr.length - 1];
    const index = roles.findIndex((ele) => ele.id === id);
    roles.splice(index, 1);
    return { code: 1, msg: '删除成功' };
  });